package cn.loioi.web.components.spring.result;

import java.util.List;

import com.github.pagehelper.PageInfo;

public class PageResult<D> extends Result<PageResult<D>> {
	private static final long serialVersionUID = 3201452877624297339L;

	private int pageNo = 1;// 当前页码
	private int pageSize = 10;// 每页多少条
	private int pages = 1;// 总共多少页

	private List<D> list;

	public PageResult() {
		super();
	}

	public PageResult(ResultError error) {
		super(error);
	}

	public PageResult(int pageNo, int pageSize) {
		super();
		this.pageNo = pageNo;
		this.pageSize = pageSize;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public void setPagesByTotal(Integer count) {
		if (count == null) pages = 1;
		this.pages = (int) Math.ceil(count / pageSize);
	}

	public void setPagesByTotal(Long count) {
		if (count == null) pages = 1;
		this.pages = (int) Math.ceil(count / pageSize);
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void format(PageInfo<D> page) {
		setList(page.getList());
		setPageNo(page.getPageNum());
		setPages(page.getPages());
		setPageSize(page.getPageSize());
	}

	public List<D> getList() {
		return list;
	}

	public void setList(List<D> list) {
		this.list = list;
		this.setResultNo(1);
	}

}
